/**
 * @author Mie Plougstrup, Anders Abildgaard, Bo Stokholm 
 * @Version 16-12-2014
 */

package tests;

import static org.junit.Assert.fail;

import java.util.ArrayList;

import modelLayer.Trailer;

import org.junit.Test;

import dbLayer.DbBusinessPartner;
import dbLayer.DbTrailer;
/**
 * Unit tests for the DbTrailer class.
 * The class will test that ability to
 * find a trailer by id and name.
 * Create, Read, Update and Delete a trailer.
 * */
public class TestDbTrailer {

	/**
	 * Find a trailer and test if it exists by finding it by trailer name.
	 */
	@Test
	public void getTrailerByName(){
		DbTrailer dbtra = new DbTrailer();

		Trailer t;

		t = dbtra.retrieveByTrailerName("HG-5639");
		if(t.getTrailerName() != null)
			System.out.println("Succesfully found trailer");
		else
			fail("IMPOSUBRUU to find trailer by name");
	}

	/**
	 * Create a list with all the trailer from the db
	 * Find the trailer in the middle of the list.
	 * Test if that trailer is not null.
	 */
	@Test
	public void getAllTrailers(){
		DbTrailer dbtra = new DbTrailer();

		ArrayList<Trailer> list = dbtra.retrieveAllTrailers();

		if(list.size() > 0){
			for(Trailer t: list){
				System.out.println(t.getTrailerName());
			}
		}else
			fail("IMPOSu to create list of all trailers.");

	}

	/**
	 * Find a trailer by id and check if it not null
	 */
	@Test
	public void getByTrailerId(){
		DbTrailer dbtra = new DbTrailer();

		Trailer t = dbtra.retrieveById(1);

		if(t.getTrailerId() != 0)
			System.out.println("Successfully found trailer by id.");
		else
			fail("ImPOSIbruu to find trailer by id.");
	}

	/**
	 * Insert trailer into db.
	 * Update the trailer name.
	 * Delete the trailer.
	 */
	@Test
	public void insertUpdateDeleteTrailerTest(){
		DbTrailer dbtra = new DbTrailer();
		DbBusinessPartner dbbp = new DbBusinessPartner();
		Trailer t = new Trailer();

		//insert trailer
		t.setTrailerName("JK-1234");
		t.setSupplier(dbbp.retrieveSupplierById(13));
		try {
			dbtra.insert(t);
		} catch (Exception e){
			System.out.println(e.getMessage());
			fail("Cant insert trailer.");
		}

		//Update trailer
		t.setTrailerName("JB-1234");
		try {
			dbtra.update(t);
		} catch (Exception e){
			fail("Cant update trailer");
		}

		//Delete trailer
		try {
			dbtra.delete(t);
		} catch (Exception e){
			fail("Failed to delete trailer");
		}
	}

	@Test
	public void TestUnusedTrailer(){
		DbTrailer dbtra = new DbTrailer();
		ArrayList<Trailer> list = new ArrayList<Trailer>();

		try{
			list = dbtra.retrieveUnusedTrailers(1);
			if(list.size() > 0)
				System.out.println("Succesfully retrieved all unused trailers.");
			else
				fail("Failed to retrieve unused trailers");
		}catch(Exception e){
			fail("Failed to retrieve unused trailers");
		}
	}
}
